package com.mxfcode.front.bbs_resource.controller;

import com.mxfcode.bbs_common.util.page.Page;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.TSUser;
import com.mxfcode.front.bbs_login.service.LoginService;
import com.mxfcode.front.bbs_resource.mapper.AttachmentMapper;
import com.mxfcode.front.bbs_resource.service.ResourceService;
import com.mxfcode.front.dto.ResourceDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 ResourceController
 * @描述 前端资源功能操作
 * @作者 mxf
 * @创建日期 2021-04-13 8:25
 **/
@Controller
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private ResourceService resourceService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private LoginService loginService;

    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public String goResourceListPage(Model model, Page page){

        page.setRows(resourceService.selectResourceRows(null));
        page.setPath("/resource/list");

        List<ResourceDTO> list = resourceService.selectResource(null, page.getOffset(), page.getLimit(), 0);
        List<Map<String,Object>> dataList = new ArrayList<>();
        if(list != null){
            for (ResourceDTO resource : list ){
                Map<String,Object> map = new HashMap<>();
                map.put("resource",resource);
                String userId = resource.getUserId();
                TSUser user = loginService.findUserById(userId);
                map.put("user",user);
                dataList.add(map);
            }
        }
        model.addAttribute("dataList",dataList);
        return "/resource";
    }
}
